$(function() {
  var curIndex = 0;

  function template(locals) {
    var buf = [];
    var jade_mixins = {};
    var jade_interp;
    var locals_for_with = locals || {};
    (function(title, txt, undefined) {
      buf.push('<div class="txt-inner"><div class="title">' + jade.escape((jade_interp = title) == null ? "" : jade_interp) + "</div>");
      (function() {
        var $$obj = txt;
        if ("number" == typeof $$obj.length) {
          for (var index = 0, $$l = $$obj.length; index < $$l; index++) {
            var val = $$obj[index];
            buf.push("<p>" + jade.escape((jade_interp = val) == null ? "" : jade_interp) + "</p>");
          }
        } else {
          var $$l = 0;
          for (var index in $$obj) {
            $$l++;
            var val = $$obj[index];
            buf.push("<p>" + jade.escape((jade_interp = val) == null ? "" : jade_interp) + "</p>");
          }
        }
      }).call(this);
      buf.push('</div><div class="bottom-bg"></div>');
    }).call(this, "title" in locals_for_with ? locals_for_with.title : typeof title !== "undefined" ? title : undefined, "txt" in locals_for_with ? locals_for_with.txt : typeof txt !== "undefined" ? txt : undefined, "undefined" in locals_for_with ? locals_for_with.undefined : typeof undefined !== "undefined" ? undefined : undefined);
    return buf.join("");
  }

  function _moveout() {
    $('#news .main-img').animate({
      left: "-200px",
      opacity: '0'
    }, function() {
      var data = NEWS[curIndex];
      $('#news .main-img').attr('src', data.img).animate({
        left: "50%",
        opacity: "0.99"
      });
    });
    $('#news .txt-container').animate({
      right: "-300px",
      opacity: "0"
    }, function() {
      var data = NEWS[curIndex];
      console.log(data);
      $('.txt-container').empty().removeData('jsp');
      $(template(data)).appendTo('.txt-container');
      $('.txt-container').jScrollPane({
        verticalDragMaxHeight: 42,
        verticalGutter: 20
      });
      $('.txt-container').animate({
        right: "0px",
        opacity: "0.99"
      });
    });
  }

  function _bindEvent() {
    $('#news .prev').click(function() {
      if (curIndex == 0) return;
      curIndex--;
      _moveout();
      //_build();
    });
    $('#news .next').click(function() {
      if (curIndex >= NEWS.length - 1) return;
      curIndex++;
      _moveout();
      //_build();
    });
  }

  function _build() {
    if (!_g.isLoaded) {
      setTimeout(_build, 500);
    } else {
      var data = NEWS[curIndex];
      $('.txt-container').empty().removeData('jsp');
      $('#news .main-img').attr('src', data.img);
      $(template(data)).appendTo('.txt-container');
      $('.txt-container').jScrollPane({
        verticalDragMaxHeight: 42,
        verticalGutter: 20
      });
    }

  }
  _build();
  _bindEvent();
});